<template>
  <div>
    <div class="date-box">
      <span class="tit">起止时间</span>
      <div class="search-time">
        <div class="picker-box">
          <el-date-picker
            v-model="startYear"
            size="small"
            type="year"
            value-format="yyyy"
            placeholder="开始年"
            prefix-icon="date-prefix"
            :clearable="false"
            :picker-options="pickerStart"
            class="el-date-picker"
            @change="changestartYear"
          >
          </el-date-picker>
          <i class="iconfont icon-riqi"></i>
        </div>
        <span class="split"></span>
        <div class="picker-box">
          <el-date-picker
            v-model="endYear"
            size="small"
            type="year"
            value-format="yyyy"
            placeholder="结束年"
            prefix-icon="date-prefix"
            :clearable="false"
            :picker-options="pickerEnd"
            class="el-date-picker"
            @change="changeendYear"
          >
          </el-date-picker>
          <i class="iconfont icon-riqi"></i>
        </div>
      </div>
    </div>
    <!-- <div v-show="selectATit !== ''" class="select-box"> -->
    <div class="select-box" v-show="showTopSize">
      <span class="tit">{{ mselectATit }}</span>
      <el-select
        v-model="size"
        size="small"
        placeholder="请选择"
        class="select"
        style="width: 100px;"
      >
        <el-option
          v-for="(item, index) in optionsA"
          :key="index"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
    <div class="select-box" v-show="tname == '法律状态趋势分析'">
      <el-select
        v-model="leg_det"
        size="small"
        placeholder="请选择"
        class="select"
        style="width: 100px;"
      >
        <el-option
          label="全量专利"
          :value="3"
        >
        </el-option>
        <el-option
          label="发明专利"
          :value="2"
        >
        </el-option>
        <el-option
          label="实用新型"
          :value="1"
        >
        </el-option>
      </el-select>
    </div>
    <div class="select-box" v-show="tname == '主要申请人排名'">
      <el-select
        v-model="range"
        size="small"
        placeholder="请选择"
        class="select"
        style="width: 100px;"
      >
        <el-option
          v-for="(item, index) in optionsB"
          :key="index"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
    <div class="select-box" v-show="tname == '国内申请人类别分析'">
      <el-select
        v-model="category"
        size="small"
        placeholder="请选择"
        class="select"
        style="width: 100px;"
      >
        <el-option
          v-for="(item, index) in optionsC"
          :key="index"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
    <div class="select-box" v-show="tname == '词云分析'">
      <el-select
        v-model="worldCloud"
        size="small"
        placeholder="请选择"
        class="select"
        style="width: 100px;"
      >
        <el-option
          v-for="(item, index) in worldCloudList"
          :key="index"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
  </div>
</template>
<script>
import { getYear, handlesShowtopsize } from "@/utils/utils";
export default {
  props: {
    selectATit: {
      type: String
    },
    param: {
      type: Object
    },
    leftTreeParentId: {
      type: Number
    },
    searchWord: {
      type: String
    },
    tname: {
      type: String
    }
  },
  data() {
    return {
      mselectATit: "",
      startYear: "",
      endYear: "",
      timeType: "isd", // 设置时间筛选类型
      pickerStart: {
        disabledDate: time => {
          return this.disabledDate(time, this.endYear, "gt");
        }
      },
      pickerEnd: {
        disabledDate: time => {
          return this.disabledDate(time, this.startYear, "lt");
        }
      },
      size: 10,
      leg_det: 3,
      optionsA: [
        {
          label: "前5",
          value: 5
        },
        {
          label: "前10",
          value: 10
        },
        {
          label: "前15",
          value: 15
        },
        {
          label: "前20",
          value: 20
        },
        {
          label: "前50",
          value: 50
        },
      ],
      range: "",
      optionsB: [
        {
          label: "全球",
          value: ""
        },
        {
          label: "国内",
          value: " AND nd=CN"
        },
        {
          label: "国外",
          value: " AND nd!=CN"
        },
      ], 
      category: 3,
      optionsC: [
        {
          label: "全量专利",
          value: 3
        },
        {
          label: "发明专利",
          value: 2
        },
        {
          label: "实用新型",
          value: 1
        },
      ], 
      worldCloud: 100,
      worldCloudList: [
        {
          label: "20条",
          value: 20
        },
        {
          label: "50条",
          value: 50
        },
        {
          label: "100条",
          value: 100
        },
      ], 
      showTopSize: true,
      myparam: null
    };
  },
  mounted() {
    this.init();
  },
  watch: {
    size() {
      // console.log("----监听size---");
      this.setSize();
      this.$emit("getChartData", {
        startYear: this.startYear,
        endYear: this.endYear,
        timeType: this.timeType,
        param: this.param,
        size: this.size
      });
    },
    leg_det() {
      // console.log("----监听leg_det---");
      this.$emit("getChartData", {
        startYear: this.startYear,
        endYear: this.endYear,
        timeType: this.timeType,
        param: this.param,
        size: this.size,
        leg_det: this.leg_det
      });
    },
    range() {
      // console.log("----监听leg_det---");
      this.$emit("getChartData", {
        startYear: this.startYear,
        endYear: this.endYear,
        timeType: this.timeType,
        param: this.param,
        size: this.size,
        range: this.range
      });
    },
    category() {
      this.$emit("getChartData", {
        startYear: this.startYear,
        endYear: this.endYear,
        timeType: this.timeType,
        param: this.param,
        size: this.size,
        category: this.category
      });
    },
    worldCloud() {
      this.$emit("getChartData", {
        startYear: this.startYear,
        endYear: this.endYear,
        timeType: this.timeType,
        param: this.param,
        size: this.worldCloud,
      });
    },
    searchWord() {
      // console.log("----监听searchWord---");
      this.setSize();
      this.$emit("getChartData", {
        startYear: this.startYear,
        endYear: this.endYear,
        timeType: this.timeType,
        param: this.param,
        size: this.size
      });
    },
    param(nv, ov) {
      if (nv != this.myparam) {
        this.myparam = nv;
        this.setSize();
        this.setTimeType();
      }
    },
    selectATit(nav) {
      this.mselectATit = this.selectATit;
      // if (this.mselectATit == "词云分析") {
      //   this.showTopSize = false;
      // }
    },
    tname(nv, ov) {
      this.showTopSize = handlesShowtopsize(nv);
      //初始化size = 10
      this.$nextTick(() => {
        this.size = 10;
      });
    },
    startYear() {
      this.$Bus.$emit("analysis_startYear", this.startYear);
      localStorage.setItem("analysis_startYear", this.startYear)
    },
    endYear() {
      this.$Bus.$emit("analysis_endYear", this.endYear);
      localStorage.setItem("analysis_endYear", this.endYear)
    },
  },
  methods: {
    init() {
      this.setSize();
      this.setTimeType();
      let _startYear = localStorage.getItem("analysis_startYear")
      let _endYear = localStorage.getItem("analysis_endYear")
      if (_startYear || _endYear) {
        this.startYear = _startYear
        this.endYear = _endYear
      } else {
        let { startYear, endYear } = getYear(10);
        this.startYear = String(startYear);
        this.endYear = String(endYear);
        localStorage.setItem("analysis_startYear", this.startYear)
        localStorage.setItem("analysis_endYear", this.endYear)
      }
    },
    showFalvState(){
        
      },
    // // 设置size参数
    setSize() {
      if (this.param?.aggs?.find(v => v.field === "an")) {
        this.mselectATit = "申请人";
        let item = this.param?.aggs?.filter(v => v.field === "an")[0];
        this.$set(item, "size", this.size);
      } else if (this.param?.aggs?.find(v => v.field === "in")) {
        this.mselectATit = "发明人";
        let item = this.param?.aggs?.filter(v => v.field === "in")[0];
        this.$set(item, "size", this.size);
      }
    },
    // 设置时间筛选类型
    setTimeType() {
      if (this.param?.aggs?.find(v => v.field === "isd")) {
        this.timeType = "isd";
      } else {
        this.timeType = "apd";
      }
    },
    changestartYear(val) {
      this.startYear = val;
      //触发父组件事件
      this.$emit("getChartData", {
        startYear: val,
        endYear: this.endYear,
        timeType: this.timeType,
        param: this.param,
        size: this.size
      });
      // 存储筛选时间
      localStorage.setItem("chooseStartTime", val);
    },
    changeendYear(val) {
      this.endYear = val;
      this.$emit("getChartData", {
        startYear: this.startYear,
        endYear: val,
        timeType: this.timeType,
        param: this.param,
        size: this.size
      });
      // 存储筛选时间
      localStorage.setItem("chooseEndTime", val);
    },
    disabledDate(time, year, type) {
      if (type === "gt") {
        if (year && year !== "") {
          if (time.getFullYear() > Number(year)) {
            return true;
          }
          return false;
        }
      } else if (type === "lt") {
        if (year && year !== "") {
          if (time.getFullYear() < Number(year)) {
            return true;
          }
          return false;
        }
      }
    }
  }
};
</script>
